package com.example.zzj.ziwuyundemo.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.zzj.ziwuyundemo.R;
import com.example.zzj.ziwuyundemo.base.BaseActivity;
import com.example.zzj.ziwuyundemo.entity.ArrivalEntity;
import com.example.zzj.ziwuyundemo.entity.AwayEntity;
import com.example.zzj.ziwuyundemo.entity.ReplyEntity;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zzj on 2016/12/1.
 */

public class SiteActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.site_back)
    ImageView siteBack;
    @InjectView(R.id.arrival)
    Button arrival;
    @InjectView(R.id.away)
    Button away;

    private String userId;
    private String pcId;
    private ReplyEntity replyEntity;
    private ArrivalEntity arrivalEntity;
    private AwayEntity awayEntity;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_site);
        ButterKnife.inject(this);
        initView();
        initData();
    }

    private void initView() {
        Intent intent = getIntent();
        userId = intent.getStringExtra("userId");
        OkHttpClient okHttpClient = new OkHttpClient();
        FormEncodingBuilder builder = new FormEncodingBuilder();
        builder.add("userId", userId);
        final Request request = new Request.Builder()
                .url("http://192.168.1.75:8080/mclcm/a/mclcm/test/getpcd")
                .post(builder.build())
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                String body = response.body().string();
                Gson gson = new Gson();
                replyEntity = new ReplyEntity();
                replyEntity = gson.fromJson(body,ReplyEntity.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pcId = replyEntity.getRecodeSendcarPlans().getId();
                    }
                });
            }
        });
    }

    private void initData() {
        siteBack.setOnClickListener(this);
        arrival.setOnClickListener(this);
        away.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.site_back:
                finish();
                break;
            case R.id.arrival:
                OkHttpClient okHttpClient = new OkHttpClient();
                final FormEncodingBuilder builder = new FormEncodingBuilder();
                builder.add("pcId", pcId);
                final Request request = new Request.Builder()
                        .url("http://192.168.1.75:8080/mclcm/a/mclcm/test/arrive")
                        .post(builder.build())
                        .build();
                Log.d("request6", request.toString());
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {

                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String arrBody = response.body().string();
                        Log.d("arrBody",arrBody);
                        Gson gson = new Gson();
                        arrivalEntity = new ArrivalEntity();
                        arrivalEntity = gson.fromJson(arrBody,ArrivalEntity.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String arrError = arrivalEntity.getArr_error();
                                switch (arrError){
                                    case "0":
                                        Toast.makeText(mContext,"没有行驶路线",Toast.LENGTH_SHORT).show();
                                        break;
                                    case "1":
                                        AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
                                        builder.setMessage("到场成功");
                                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();
                                            }
                                        });
                                        builder.create().show();
                                        break;
                                    case "2":
                                        Toast.makeText(mContext,"还没有到达现场",Toast.LENGTH_SHORT).show();
                                        break;
                                    case "3":
                                        Toast.makeText(mContext,"还没有出发",Toast.LENGTH_SHORT).show();
                                        break;
                                }
                            }
                        });
                    }
                });
                break;
            case R.id.away:
                OkHttpClient okHttpClient1 = new OkHttpClient();
                final FormEncodingBuilder builder1 = new FormEncodingBuilder();
                builder1.add("pcId", pcId);
                final Request request1 = new Request.Builder()
                        .url("http://192.168.1.75:8080/mclcm/a/mclcm/test/leave")
                        .post(builder1.build())
                        .build();
                Log.d("request7", request1.toString());
                okHttpClient1.newCall(request1).enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {

                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String awayBody = response.body().string();
                        Log.d("awayBody",awayBody);
                        Gson gson = new Gson();
                        awayEntity = new AwayEntity();
                        awayEntity = gson.fromJson(awayBody,AwayEntity.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String awayError = awayEntity.getLeave_error();
                                switch (awayError){
                                    case "0":
                                        Toast.makeText(mContext,"没有到达客户现场",Toast.LENGTH_SHORT).show();
                                        break;
                                    case "1":
                                        AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
                                        builder.setMessage("离场成功");
                                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();
                                            }
                                        });
                                        builder.create().show();
                                        break;
                                    case "2":
                                        Toast.makeText(mContext,"不满足离开的条件",Toast.LENGTH_SHORT).show();
                                        break;
                                }
                            }
                        });

                    }
                });
                break;
        }
    }
}
